package navigation.wzg.com.navigation.activities;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mob.MobSDK;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.http.Http_method;
import navigation.wzg.com.navigation.http.SMSPost;

public class Register extends AppCompatActivity {

    @BindView(R.id.toolbar_login)
    Toolbar toolbarLogin;
    @BindView(R.id.register_user)
    EditText registerUser;
    @BindView(R.id.register_pwd_1)
    EditText registerPwd1;
    @BindView(R.id.register_pwd_2)
    EditText registerPwd2;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.yzm)
    Button yzm;
    @BindView(R.id.register)
    Button register;
    @BindView(R.id.activity_register)
    LinearLayout activityRegister;
    @BindView(R.id.T_phone)
    TextInputLayout TPhone;

    @BindView(R.id.T_pwd2)
    TextInputLayout TPwd2;
    @BindView(R.id.T_user)
    TextInputLayout TUser;
    @BindView(R.id.T_pwd1)
    TextInputLayout TPwd1;

    static Activity activity1;
    @BindView(R.id.edit_yzm)
    EditText editYzm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        activity1 = this;
        Toast.makeText(this, "账号仅支持由字母，数字，@和.组成", Toast.LENGTH_SHORT).show();

        setSupportActionBar(toolbarLogin);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbarLogin.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
            }
        });

        // 创建EventHandler对象
        EventHandler eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable) data;
                    String msg = throwable.getMessage();
                    Toast.makeText(Register.this, msg, Toast.LENGTH_SHORT).show();
                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        // 处理你自己的逻辑
                    }
                }
            }
        };
        // 注册监听器
//        SMSSDK.registerEventHandler(eventHandler);


        //给tishi1设置计数，长度位11位
        TPhone.setCounterEnabled(true);
        TPhone.setCounterMaxLength(11);


        match(registerPwd2, TPwd2, 1, "两次密码不匹配");
        match(phone, TPhone, 2, "请填写正确的手机号码");
    }


    @OnClick({R.id.yzm, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yzm:
                if (phone.length() == 11) {
                    MobSDK.init(Register.this, "218254476a474", "80f66a37ec0b7af5369dd157e9a44f84");
                    SMSSDK.getVerificationCode("86", phone.getText().toString().trim());
                    yzm.setEnabled(false);
                    Toast.makeText(this, "重新获取验证码请等待30s", Toast.LENGTH_SHORT).show();
                    countdown();
                } else {
                    Toast.makeText(this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.register:
                registers();
                break;
        }
    }

    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.out, R.anim.up);
    }

    /**
     * 倒计时的实现以及防止频繁点击获取验证码
     */
    public void countdown() {
        final int[] a = {30};
        final Timer time = new Timer();
        TimerTask ttt = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        a[0]--;
                        yzm.setText("" + a[0] + "s后可重发");
                        if (a[0] == 1) {
                            time.cancel();
                            yzm.setEnabled(true);
                            yzm.setText("获取短信验证码");
                        }
                    }
                });
            }
        };
        time.schedule(ttt, 1000, 1000);
    }

    /**
     * 基于TextInputLayout的错误提示
     */
    public void match(final EditText edtext, final TextInputLayout t, final int i, final String error) {
        edtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                t.setErrorEnabled(true);
                switch (i) {
                    case 1:
                        if (registerPwd1.getText().length() <= registerPwd2.getText().length()) {
                            if (!registerPwd1.getText().toString().equals(registerPwd2.getText().toString())) {
                                edtext.setError(error);
                            }
                        }
                        break;
                    case 2:
                        if (phone.length() > 11) {
                            edtext.setError(error);
                        }
                        break;
                }

            }
        });
    }

    /**
     * 注册按钮的点击事件
     */
    public void registers() {
        if (registerUser.getText().length() < 6) {
            TUser.setErrorEnabled(true);
            registerUser.setError("账号不能小于6位");
        } else {
            if (registerPwd1.getText().length() < 6) {
                TPwd1.setErrorEnabled(true);
                registerPwd1.setError("密码不能小于6位");
            } else {
                if (!registerPwd1.getText().toString().equals(registerPwd2.getText().toString())) {
                    TPwd2.setErrorEnabled(true);
                    registerPwd2.setError("两次密码不匹配");
                } else {
//                    Http_method.ADD(registerUser.getText().toString(),registerPwd1.getText().toString(),Register.this);
//                    Toast.makeText(this, "验证码", Toast.LENGTH_SHORT).show();
                    if (editYzm.length() < 4) {
                        Toast.makeText(activity1, "请输入完整的验证码", Toast.LENGTH_SHORT).show();
                    } else {
//                        boolean aa = SMSPost.SmsPost(phone.getText().toString(), editYzm.getText().toString().trim(), activity1);
//                        Toast.makeText(activity1, aa + "", Toast.LENGTH_SHORT).show();
//                        if (aa) {
                        Http_method.ADD(registerUser.getText().toString(),registerPwd1.getText().toString(),Register.this);
//                        }
                    }
                }
            }
        }
    }
}
